﻿
function GetChart(charttype, url, element, title) {
    var _datasuore = [];//统计数据（[ {value:0, name:''}]）
    var _titledata = [];//标题数据
    var _x_data = [];//x轴分类数据
    var _x_footdata = [];//x轴分类
    var index = 0;
    var dom = document.getElementById(element);
    var myChart = echarts.init(dom);
    var option = "";
    $.ajax({
        type: "Post",
        url: url,
        data: "json",
        success: function (data) {
            if (charttype == "pie") {//饼图
                $(data).each(function (j, k) {
                    _datasuore.push({ value: k.value, name: k.key });
                    _titledata.push(k.key);
                });
                option = {
                    title: {
                        text: title,
                        //subtext: '纯属虚构',
                        x: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        x: 'left',
                        data: _titledata
                    },
                    calculable: true,
                    series: [
                        {
                            name: title,
                            type: charttype,
                            radius: '55%',
                            center: ['50%', '60%'],
                            data: _datasuore
                        }
                    ]
                };
            } else if (charttype == "bar" || charttype == "line") {//折线图或者柱状图
                $(data).each(function (j, k) {
                    _titledata.push(k.key);
                    var list = [];
                    $(k.data).each(function (i, a) {
                        list.push(a.value);
                        if (index == 0) {//只取一次X轴数据
                            _x_footdata.push(a.key);
                        }
                    });
                    _x_data.push({
                        name: k.key,
                        type: charttype,
                        data: list,
                        markPoint: {
                            data: [
                                { type: 'max', name: '最大值' },
                                { type: 'min', name: '最小值' }
                            ]
                        }
                    });
                    index++;
                });
                option = {
                    title: {
                        text: title
                        //subtext: '纯属虚构'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: _titledata
                    },
                    calculable: true,
                    xAxis: [
                        {
                            type: 'category',
                            data: _x_footdata
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value'
                        }
                    ],
                    series: _x_data
                };
            }
            //使用刚才的配置
            myChart.setOption(option);
        },
        error: function (e) {
            alert("出错了,请稍后再试!");

        }
    });
}